package com.haixiaoke.common.oss.service;

import com.aliyun.oss.OSSClient;
import com.haixiaoke.common.oss.config.OssConfig;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * 阿里云OSS对象存储
 *
 * @author shun
 * @createDate: 2021年3月23日 下午2:49:08
 * @version: 1.0
 */
@Service
public class OSSService {
    @Autowired
    private OSSClient ossClient;
    @Autowired
    private OssConfig ossConfig;

    /**
     * 上传文件
     *
     * @param bucketName oss桶名称
     *                   文件
     * @param type       文件业务类型
     * @return 文件key
     * @author shun
     * @date 2021年3月24日
     */
    public String putFile(String bucketName, MultipartFile mfile, String type) {
        if (StringUtils.isEmpty(bucketName)) {
            bucketName = ossConfig.getOssBucketName();
        }
        File temp = new File("temp_" + getNewName(mfile.getOriginalFilename()));
        StringBuilder fileKey = new StringBuilder();
        fileKey.append(type);
        fileKey.append("/").append(getNewName(mfile.getOriginalFilename()));
        try (FileOutputStream fos = new FileOutputStream(temp);
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            bos.write(mfile.getBytes());
            bos.flush();

            // 上传
            ossClient.putObject(bucketName, fileKey.toString(), temp);
            return "https://xj-saas-images.oss-cn-qingdao.aliyuncs.com/" + fileKey;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (temp.exists()) {
                FileUtils.deleteQuietly(temp);
            }
        }
    }

    private String getNewName(String filename) {
        int indx = filename.lastIndexOf(".");
        String suffix = filename.substring(indx, filename.length());
        return (getDate() + System.currentTimeMillis() + suffix);
    }

    private String getDate() {
        return DateFormatUtils.format(new Date(), "yyyyMMdd");
    }

    public String putFileLock(String bucketName, MultipartFile mfile, String type) {
        if (StringUtils.isEmpty(bucketName)) {
            bucketName = ossConfig.getOssBucketName();
        }
        File temp = new File("temp_" + getNewName(mfile.getOriginalFilename()));
        StringBuilder fileKey = new StringBuilder();
        fileKey.append(type);
        fileKey.append("/").append(getNewName(mfile.getOriginalFilename()));
        try (FileOutputStream fos = new FileOutputStream(temp);
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            bos.write(mfile.getBytes());
            bos.flush();

            // 上传
            ossClient.putObject(bucketName, fileKey.toString(), temp);
            return "https://devices-smart-lock.oss-cn-beijing.aliyuncs.com/" + fileKey;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (temp.exists()) {
                FileUtils.deleteQuietly(temp);
            }
        }
    }
}
